package com.coderman.infosys.auth.app.facadeimpl.sdk;

import com.coderman.infosys.auth.api.dto.sdk.RoleAuthorityDTO;
import com.coderman.infosys.auth.api.dto.sdk.UserAuthorityDTO;
import com.coderman.infosys.auth.api.facade.sdk.AuthPlatQueryAPI;
import com.coderman.infosys.auth.app.convert.AuthorityConvert;
import com.coderman.infosys.auth.domain.bo.authority.RoleAuthAggregateBO;
import com.coderman.infosys.auth.domain.bo.authority.UserAuthAggregateBO;
import com.coderman.infosys.auth.domain.enums.errors.AuthorityErrorEnum;
import com.coderman.infosys.auth.domain.gataway.AuthorityQueryGataWay;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * Description:
 * date: 2022/1/9
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class AuthPlatQueryAPIImpl implements AuthPlatQueryAPI {

    @Autowired
    public AuthorityQueryGataWay authorityQueryGataWay;

    @Override
    public ResultDataDto<UserAuthorityDTO> queryAuthorityWithUser(Long userId, String systemCode) {
        UserAuthAggregateBO userAuthAggregateBO = authorityQueryGataWay.queryAuthorityWithUser("", userId, systemCode);
        if(Objects.isNull(userAuthAggregateBO)){
            return ResultDataDto.fail(AuthorityErrorEnum.USER_AUTH_NOF_FUND_ERROR);
        }
        UserAuthorityDTO userAuthorityDTO = AuthorityConvert.INSTANCE.convert2DTO(userAuthAggregateBO.getAuthAggregateBO());
        userAuthorityDTO.setUserId(userId);
        userAuthorityDTO.setRoleCodeList(userAuthAggregateBO.getRoleCodeList());
        userAuthorityDTO.setRoleGroupCodeList(userAuthAggregateBO.getRoleGroupCodeList());
        return ResultDataDto.success(userAuthorityDTO);
    }

    @Override
    public ResultDataDto<RoleAuthorityDTO> queryAuthorityWithRole(String roleCode) {

        RoleAuthAggregateBO roleAuthAggregateBO = authorityQueryGataWay.queryAuthorityWithRole("",roleCode);
        if(Objects.isNull(roleAuthAggregateBO)){
            return ResultDataDto.fail(AuthorityErrorEnum.ROLE_AUTH_NOF_FUND_ERROR);
        }
        RoleAuthorityDTO roleAuthorityDTO = AuthorityConvert.INSTANCE.convert2RoleAuthDTO(roleAuthAggregateBO.getAuthAggregateBO());
        roleAuthorityDTO.setRoleCode(roleCode);
        return ResultDataDto.success(roleAuthorityDTO);
    }
}
